package com.mq.dpca.ymtc;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;

import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.MQException;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;

public class TEST_RECEIVER implements MessageListener {

	public static void main(String[] args) throws MQException {
		sendMsg2();
	}

	public static void sendMsg2() {
		MQQueueConnectionFactory connectionFactory = null;
		QueueConnection queueConn = null;
		QueueSession queueSession = null;

		
		/*mc = MQConfig.getInstance();
		//创建连接工厂
		mcf = new MQQueueConnectionFactory();
		mcf.setConnectionNameList(mc.getMQ_HOST_NAME());
		mcf.setChannel(mc.getMQ_CHANNEL());
		mcf.setPort(Integer.valueOf(mc.getMQ_PROT()));
		mcf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
		mcf.setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT);
		mcf.setCCSID(Integer.parseInt(mc.getMQ_CCSID()));
		mcf.setQueueManager(mc.getMQ_MANAGER());
		//获取连接对象
		qconn = mcf.createQueueConnection();
		session = qconn.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
	    queue = session.createQueue(mc.getMQ_QUEUE_NAME());
        receiver = session.createReceiver(queue);
        receiver.setMessageListener(receive);
        qconn.start();
        while (true) {}*/
        
		try {
			connectionFactory = new MQQueueConnectionFactory();
			connectionFactory
					.setConnectionNameList("10.22.15.112");
			connectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
			connectionFactory.setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT);
			connectionFactory.setQueueManager("YMTC_QMGR_001");
			connectionFactory.setChannel("TEST_SVRCHL_001");
			queueConn = connectionFactory.createQueueConnection(null, null);
			queueSession = queueConn.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			Queue queue = queueSession.createQueue("TEST_ESBOUT_001");

			QueueReceiver receiver = queueSession.createReceiver(queue);

			TEST_RECEIVER instance = new TEST_RECEIVER();
			// 设置侦听器
			receiver.setMessageListener(instance);
			// 启动侦听
			queueConn.start();

			while (true) {
			}

		} catch (Exception e) {
			e.printStackTrace();
			if (null != queueConn) {
				try {
					queueConn.close();
				} catch (JMSException e1) {
					e1.printStackTrace();
				}
			}
			if (queueSession != null) {
				try {
					queueSession.close();
				} catch (JMSException e1) {
					e1.printStackTrace();
				}
			}
		} 

	}

	@Override
	public void onMessage(Message message) {

		if (message == null)
			return;
		if (message instanceof JMSTextMessage) {
			try {
				System.out.println(message.getStringProperty("Esbheader"));
				System.out.println(((JMSTextMessage) message).getText());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
		}

	}

}
